<?
/**
 * @package    phpdbo-api
 * @subpackage phpmydb
 * @author     Mike Green <mdgreen@gmail.com>
 * @copyright  Copyright (c) 2010, Mike Green
 * @license    http://opensource.org/licenses/gpl-2.0.php GPLv2
 */
(basename($_SERVER['PHP_SELF']) == basename(__FILE__)) && die('Access Denied');

// Attempt to load the module
putenv('LD_LIBRARY_PATH="/opt/sqlanywhere12/lib32"');
if (!extension_loaded('sqlanywhere')) {
	$rc = dl('sqlanywhere.so');
	if (!($rc === true)) {
		die('sqlanywhere extension is missing');
		}
	}

class phpsqlanywheredb {

var $_dbh = false;
var $_db_name;
var $_persist = false;
var $connected = false;

/**
 * Constructor
 */
function __construct($db_host = null, $db_name = null, $db_user = null,
                     $db_pass = null, $db_port = null) {
	if (!empty($db_host) and !empty($db_user) and !empty($db_pass)) {
		$this->db_connect($db_host, $db_name, $db_user, $db_pass);
		}
	}

/**
 * Returns the number of rows affected by the last INSERT, UPDATE, or DELETE
 */
function db_affected_rows() {
	return sasql_affected_rows($this->_dbh);
	}

/**
 * Connect to the specified database.
 */
function db_connect($dbh = null, $dbn = null, $dbu = null, $dbp = null) {
	if ($this->_persist === true) {
		$func = 'sasql_pconnect';
		} else {
		$func = 'sasql_connect';
		}
	$conStr = array();
	if (is_string($dbh) and strlen($dbh)) {
		$conStr[] = 'host=' . $dbh;
		}
	if (is_string($dbn) and strlen($dbn)) {
		$conStr[] = 'dbn=' . $dbn;
		}
	if (is_string($dbu) and strlen($dbu)) {
		$conStr[] = 'uid=' . $dbu;
		}
	if (is_string($dbp) and strlen($dbp)) {
		$conStr[] = 'pwd=' . $dbp;
		}
	$str = implode(';', $conStr);
	if (!$dbh = $func($str)) { return false; }

	$this->_dbh =& $dbh;
	$this->connected = true;
	return true;
	}

/**
 * Move the database cursor to row $row on the select query qid.
 */
function db_data_seek($qid, $row) {
	return sasql_data_seek($qid, $row);
	}

/**
 * Disconnect from the database connection.
 */
function db_disconnect() {
	$this->connected = false;
	return sasql_close($this->_dbh);
	}

/**
 * Get the most recent error message.
 */
function db_error() {
	return sasql_error();
	}

/**
 * Fetch a row and return as an array with field name indices
 */
function db_fetch_array($qid = false) {
	if ($qid === false) { return false; }
	return sasql_fetch_assoc($qid);
	}

/**
 * Fetch a row and convert it into an object.
 */
function db_fetch_object($qid = false) {
	if ($qid === false) { return false; }
	return sasql_fetch_object($qid);
	}

/**
 * Fetch a row as an array with numeric indices
 */
function db_fetch_row($qid = false) {
	if ($qid === false) { return false; }
	return sasql_fetch_row($qid);
	}

/**
 * Fetch field flags
 */
function db_field_flags($qid = false, $field_num = '') {
	sasql_fetch_field($qid, $field_num);
	}

/**
 * Fetch a field length
 */
function db_field_length($qid = false, $field_num = '') {
	if ($qid === false) { return false; }
	if (!is_numeric($field_num)) { return false; }
	$o = sasql_fetch_field($qid, $field_num);
	return (isset($o->length)) ? $o->length : false;
	}

/**
 * Fetch a field name
 */
function db_field_name($qid = false, $field_num = '') {
	if ($qid === false) { return false; }
	if (!is_numeric($field_num)) { return false; }
	$o = sasql_fetch_field($qid, $field_num);
	return (isset($o->name)) ? $o->name : false;
	}

/**
 * Return field number of table field name $field from query result qid.
 */
function db_field_num($qid = false, $field = null) {
	if (!is_string($field) or !strlen($field)) { return false; }
	if ($qid === false) { return false; }
	for ($j=0; $j < $this->db_num_fields(); $j++) {
		if (strtolower($this->db_field_name($qid, $j)) == strtolower($field)) {
			return $j;
			}
		}
	return false;
	}

/**
 * Get field type
 */
function db_field_type($qid = false, $field_num = '') {
	if ($qid === false) { return false; }
	if (!is_numeric($field_num)) { return false; }
	$o = sasql_fetch_field($qid, $field_num);
	return (isset($o->type)) ? $o->type : false;
	}

/**
 * Free results from last query
 */
function db_free_result($qid=false) {
	return sasql_free_result($qid);
	}

/**
 * Fetch the insert id generated by the most recent insert statement
 */
function db_insert_id() {
	return sasql_insert_id($this->_dbh);
	}

/**
 * Fetch a list of field names.
 */
function db_list_fields($table = '') {
	return false;
	}

/**
 * Return a list of database tables.
 */
function db_list_tables($database) {
	return false;
	}

/**
 * Returns the number of fields in a select query qid.
 */
function db_num_fields($qid = false) {
	return sasql_field_count($this->_dbh);
	}

/**
 * Return the number of rows returned from the SELECT query qid.
 */
function db_num_rows($qid = false) {
	return sasql_num_rows($qid);
	}

/**
 * Ping the current database connection
 */
function db_ping() {
	return false;
	}

/**
 * Perform a query, using the current database handle.
 */
function db_query($query) {
	return sasql_query($this->_dbh, $query);
	}

/**
 * Fetch results from a row.
 */
function db_result($qid = false, $row = '', $field = '') {
	if ($qid === false) { return false; }
	if (!sasql_data_seek($qid, $res, $row)) { return false; }
	return sasql_fetch_field($res, $field);
	}

/**
 * Select the database subsequent operations will be peformed on.
 */
function db_select($db_name = null) { return false; }

/**
 * Take a query identifier and create an array of associative arrays.
 */
function query_hash_rows($qid = false, $format = 'associative') {
	if ($qid === false) { return false; }
	$tmp_ar = array();
	if ($this->db_num_rows($qid) < 1) { return $tmp_ar; }
	for($j = 0; $j < $this->db_num_rows($qid); $j++) {
		if ($format == 'associative') {
			$tmp_ar[] = $this->db_fetch_array($qid);
			} else {
			$tmp_ar[] = $this->db_fetch_row($qid);
			}
		}
	return $tmp_ar;
	}
}
?>
